package com.zxl.always.on.display.configs.bean;

import android.text.TextUtils;

import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.configs.AdsConfigsHelper;
import com.zxl.always.on.display.configs.ChannelHelper;
import com.zxl.always.on.display.tools.AppUtils;
import com.zxl.always.on.display.tools.LogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.zxl.always.on.display.configs.AdsConfigsHelper.getConfigParams;

/**
 * Created by Administrator on 2018/1/2.
 */

public class InternalAdsBean {


    private boolean sIsInit = false;

    private boolean sIsInitContent = false;

    private boolean isShowMainNativeAds;

    private boolean isShowSettingBannerAds;

    private boolean isShowSettingOfferAds;

    private boolean isShowMainShuffleAds;

    private boolean isShowLockNotifyAds;

    private boolean isShowLockCleanAds;

    private List<Integer> mAdsContent = new ArrayList<>();

    public InternalAdsBean() {
        if (ChannelHelper.isWaps()) {
            isShowMainNativeAds = true;
            isShowSettingBannerAds = true;
            isShowSettingOfferAds = true;
            isShowMainShuffleAds = true;
            isShowLockNotifyAds = true;
            isShowLockCleanAds = true;
            mAdsContent.add(AdsConfigsHelper.ADS_TYPE_WAPS);
        } else if (ChannelHelper.isXm()) {
            isShowMainNativeAds = true;
            isShowSettingBannerAds = true;
            isShowSettingOfferAds = false;
            isShowMainShuffleAds = true;
            isShowLockNotifyAds = true;
            isShowLockCleanAds = true;
            mAdsContent.add(AdsConfigsHelper.ADS_TYPE_XM);
        }
    }

    public void parse() {
        try {
            int vc = AppUtils.getVersionCodeByPkgName(ApplicationHelper.getContext(), ApplicationHelper.getContext().getPackageName());
            String value = getConfigParams("ads_info" + vc);
            if (TextUtils.isEmpty(value)) {
                value = getConfigParams("ads_info");
            }
            if (!TextUtils.isEmpty(value)) {
                LogUtils.d("value : " + value);
                String key = new JSONObject(value).optString(ChannelHelper.getChannel());
                value = getConfigParams(key);
                if (!TextUtils.isEmpty(value)) {
                    JSONObject jsonObject = new JSONObject(value);
                    isShowMainNativeAds = jsonObject.optBoolean("isShowMainNativeAds");
                    isShowSettingBannerAds = jsonObject.optBoolean("isShowSettingBannerAds");
                    isShowSettingOfferAds = jsonObject.optBoolean("isShowSettingOfferAds");
                    isShowMainShuffleAds = jsonObject.optBoolean("isShowMainShuffleAds");
                    isShowLockCleanAds = jsonObject.optBoolean("isShowLockCleanAds");
                    isShowLockNotifyAds = jsonObject.optBoolean("isShowLockNotifyAds");
                    sIsInit = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void parseContent() {
        try {
            int vc = AppUtils.getVersionCodeByPkgName(ApplicationHelper.getContext(), ApplicationHelper.getContext().getPackageName());
            String value = getConfigParams("ads_content" + vc);
            if (TextUtils.isEmpty(value)) {
                value = getConfigParams("ads_content");
            }
            if (!TextUtils.isEmpty(value)) {
                LogUtils.d("value : " + value);
                JSONArray jsonArray = new JSONObject(value).optJSONArray(ChannelHelper.getChannel());
                if (jsonArray.length() > 0) {
                    mAdsContent.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        mAdsContent.add(jsonArray.optInt(i));
                    }
                }
                sIsInitContent = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public boolean isShowMainShuffleAds() {
        if (!sIsInit) {
            parse();
        }
        return isShowMainShuffleAds;
    }

    public boolean isShowMainNativeAds() {
        if (!sIsInit) {
            parse();
        }
        return isShowMainNativeAds;
    }

    public boolean isShowLockCleanAds() {
        if (!sIsInit) {
            parse();
        }
        return isShowLockCleanAds;
    }

    public boolean isShowLockNotifyAds() {
        if (!sIsInit) {
            parse();
        }
        return isShowLockNotifyAds;
    }


    public boolean isShowSettingBannerAds() {
        if (!sIsInit) {
            parse();
        }
        return isShowSettingBannerAds;
    }

    public boolean isShowSettingOfferAds() {
        if (!sIsInit) {
            parse();
        }
        return isShowSettingOfferAds;
    }

    public List<Integer> getShuffleAdsContent() {
        if (!sIsInitContent) {
            parseContent();
        }
        return mAdsContent;
    }

}
